
import axios from "axios";

export default (config,errorHanlder) => {
    const http = axios.create({
        timeout: 10000,
        baseURL: process.env.NODE_ENV === "development" ? "" : "http://bawei.com",
        ...config
    });
    http.interceptors.request.use((config) => {
      
        return {
            ...config,
            headers:{
                ...config.headers,
                token:"token"
            }
        }
    },(error) => {
        return Promise.reject(error);
    });
    http.interceptors.response.use(({data}) => {
       if(data.code === "FAIL"){
         errorHanlder(data.msg);
         return Promise.reject(data);
       }
        return data;
    },(error) => {
        if(error.code === "ECONNABORTED"){
            errorHanlder("网络不好，请刷新重试~");
            return Promise.reject(error);
        }
        errorHanlder(error.response?.data?.msg);
        return Promise.reject(error);
    })

    return http;
}

